package vip.xiaonuo.biz.modular.goods.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsImpItem;
import vip.xiaonuo.biz.modular.goods.param.GoodsImpQryDto;
import vip.xiaonuo.biz.modular.goods.result.GoodsImpPageResult;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsImpItemService;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsImpService;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.util.CommonDownloadUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * @author 墨塵
 * @since 2023/11/7
 */
@Api(tags = "物品导入控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
@RequestMapping("/biz/goodsImp/**")
public class BizGoodsImpController {

    @Resource
    private BizGoodsImpService bizGoodsImpService;

    @Resource
    private BizGoodsImpItemService bizGoodsImpItemService;


    @ApiOperationSupport
    @ApiOperation("获取物品导入信息分页")
    @PostMapping("/getPageList")
    public CommonResult<Page<GoodsImpPageResult>> getPageList(@RequestBody GoodsImpQryDto dto) {
        return CommonResult.data(bizGoodsImpService.getPageList(dto));
    }

    @ApiOperationSupport
    @ApiOperation("导出模板")
    @PostMapping(value = "/exportGoodsTmpl", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportGoodsTmpl(HttpServletResponse response) {
        try {
            String path = "/tmpl/物品导入模板.xlsx";
            InputStream inputStreamClassPath = new ClassPathResource(path).getInputStream();
            File tempFile = new File("物品导入模板.xlsx");
            FileUtil.writeFromStream(inputStreamClassPath, tempFile);
            CommonDownloadUtil.download(tempFile, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException("物品导入模板导出失败");
        }
    }

    @ApiOperationSupport
    @ApiOperation("导出试剂模板")
    @PostMapping(value = "/exportReagentTmpl", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportReagentTmpl(HttpServletResponse response) {
        try {
            String path = "/tmpl/试剂导入模板.xlsx";
            InputStream inputStreamClassPath = new ClassPathResource(path).getInputStream();
            File tempFile = new File("试剂导入模板.xlsx");
            FileUtil.writeFromStream(inputStreamClassPath, tempFile);
            CommonDownloadUtil.download(tempFile, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException("试剂导入模板导出失败");
        }
    }

    @ApiOperationSupport
    @ApiOperation("物品导入")
    @PostMapping("/impGoods")
    public CommonResult<String> impGoods(@RequestPart("file") MultipartFile file) {
        int count = bizGoodsImpService.impGoods(file);
        return CommonResult.data(count + "");
    }

    @ApiOperationSupport
    @ApiOperation("试剂导入")
    @PostMapping("/impReagent")
    public CommonResult<String> impReagent(@RequestPart("file") MultipartFile file) {
        int count = bizGoodsImpService.impReagent(file);
        return CommonResult.data(count + "");
    }

    @ApiOperationSupport
    @ApiOperation("获取物品导入明细分页")
    @PostMapping("/getItemList")
    public CommonResult<List<BizGoodsImpItem>> getItemList(@RequestBody GoodsImpQryDto dto) {
        return CommonResult.data(bizGoodsImpItemService.getList(dto));
    }

    @ApiOperationSupport
    @ApiOperation("解析单条")
    @GetMapping("/analById")
    public CommonResult<String> analById(String itemId) {
        bizGoodsImpItemService.analById(itemId);
        return CommonResult.ok();
    }
}
